/*
 * Copyright (c) 2007, Tim Stack
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Tim Stack nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY TIM STACK ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL TIM STACK BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>

#include "reflecxx.h"

#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif

const struct reflecxx_buffer REFLECXX_EMPTY_BUFFER = {
    NULL, 0, 0, 0, RBF_RESIZABLE
};

static void *in_reflecxx_malloc(size_t s)
{
    return malloc(s);
}

static void in_reflecxx_free(void *m)
{
    free(m);
}

static void *in_reflecxx_realloc(void *m, size_t s)
{
    return realloc(m, s);
}

void *(*reflecxx_malloc)(size_t) = in_reflecxx_malloc;
void (*reflecxx_free)(void *) = in_reflecxx_free;
void *(*reflecxx_realloc)(void *, size_t) = in_reflecxx_realloc;

struct reflecxx_buffer reflecxx_buffer_string(char *data)
{
    struct reflecxx_buffer retval;

    memset(&retval, 0, sizeof(retval));
    retval.rb_data = data;
    retval.rb_used = retval.rb_max_size = strlen(data) + 1;
    
    return retval;
}

size_t reflecxx_sizeof(struct reflecxx_full_type *rft)
{
    size_t retval = 0;

    assert(rft != NULL);
    assert(rft->rft_type >= RT_UNKNOWN);
    assert(rft->rft_type < RT__MAX);
    
    switch (rft->rft_type) {
    case RT_UNKNOWN:
	break;
    case RT_CHAR:
    case RT_UNSIGNED_CHAR:
	retval = sizeof(char);
	break;
    case RT_SHORT:
    case RT_UNSIGNED_SHORT:
	retval = sizeof(short);
	break;
    case RT_INT:
    case RT_UNSIGNED_INT:
    case RT_ENUM:
	retval = sizeof(int);
	break;
    case RT_LONG:
    case RT_UNSIGNED_LONG:
	retval = sizeof(long);
	break;
    case RT_LONG_LONG:
    case RT_UNSIGNED_LONG_LONG:
	retval = sizeof(long long);
	break;
    case RT_FLOAT:
	retval = sizeof(float);
	break;
    case RT_DOUBLE:
	retval = sizeof(double);
	break;
    case RT_POINTER:
    case RT_STRING:
    case RT_FUNCTION:
	retval = sizeof(void *);
	break;
    case RT_BOOL:
	retval = 1;
	break;
    case RT_VOID:
	break;
    case RT_STRUCT:
	retval = rft->rft_type_info.st->rs_byte_length;
	break;
    case RT_ARRAY:
	retval = (rft->rft_type_info.arr->ra_hi -
		  rft->rft_type_info.arr->ra_lo + 1) *
	    reflecxx_sizeof(&rft->rft_type_info.arr->ra_element_type);
	break;
    }
    
    return retval;
}
